package ThreadPool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class Do {
    public static void main(String[] args) throws InterruptedException{
        ThreadPool threadPool = new ThreadPool(10);
        for (int i = 0; i < 100; i++) {
            int n = i;
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello " + n);
                }
            });
        }
    }
}
public class ThreadPool {
    public BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();
    public ThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                while (true) {
                    try {
                        Runnable runnable = blockingQueue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        blockingQueue.put(runnable);
    }
}

